package com.example.demo.bizhan.reflect;

import org.junit.Test;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import static java.lang.System.*;

/**
 * @description: xxx
 * @time: 2020-10-26 22:02
 * @author: baojinlong
 **/
public class ReflectDemoTest {
    public static void main(String[] args) {

    }

    @Test
    public void test05() throws Exception {
        List<String> teachers = new ArrayList<>();
        teachers.add("a");
        teachers.add("b");
        teachers.add("c");
        // 下面通过放射进行加入int类型的值
        Class<? extends List> teacherClazz = teachers.getClass();
        Method add = teacherClazz.getDeclaredMethod("add", Object.class);
        add.invoke(teachers, "d老师");
        add.invoke(teachers, 11111);
        out.println(teachers);
    }


    @Test
    public void test04() throws Exception {
        Class<Child> childClass = Child.class;
        out.println(childClass);
        Class<? super Child> parentClass = childClass.getSuperclass();
        out.println(parentClass);
        out.println(parentClass.getSuperclass());
    }

    @Test
    public void test03() throws Exception {
        out.println(Integer.class);
        out.println(Integer.TYPE);
    }

    @Test
    public void test02() throws Exception {
        String name = "windy";
        Class<? extends String> aClass = name.getClass();
        out.println(aClass);

        Class<String> stringClass = String.class;
        out.println(stringClass);
    }

    @Test
    public void test01() throws Exception {
        // 使用class.forName获取class对象
        Class<?> clazz = Class.forName("java.lang.Object");
        out.println(clazz);
        out.println("获取所有方法");
        Method[] declaredMethods = clazz.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            out.println(declaredMethod);
        }
        out.println("获取所有public方法");
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            out.println(method);
        }
    }
}
